package com.jetpack_sunflower.viewmodels

import android.content.Context
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.jetpack_sunflower.data.Plant
import com.jetpack_sunflower.data.PlantRespository

class PlantListViewModel internal constructor(
        private val plantRespository: PlantRespository
) : ViewModel() {

    private val growZonNumber = MutableLiveData<Int>()

    private val plantList = MediatorLiveData<List<Plant>>()

    init {
        growZonNumber.value = NO_GROW_ZONE

        val livePlantList = Transformations.switchMap(growZonNumber) {
            if (it == NO_GROW_ZONE) {
                plantRespository.getPlants()
            } else {
                plantRespository.getPlantsWithGrowZoneNumber(it)
            }
        }
        plantList.addSource(livePlantList, plantList::setValue)
    }

    fun getPlants() =plantList

    fun setGrowZoneNumber(num:Int){
        growZonNumber.value = num
    }

    fun clearGrowZonNumber(){
        growZonNumber.value = NO_GROW_ZONE
    }

    fun isFilted() = growZonNumber.value != NO_GROW_ZONE

    companion object {
        private const val NO_GROW_ZONE = -1
    }

}


/**
 * private val growZoneNumber = MutableLiveData<Int>()

private val plantList = MediatorLiveData<List<Plant>>()

init {
growZoneNumber.value = NO_GROW_ZONE

val livePlantList = Transformations.switchMap(growZoneNumber) {
if (it == NO_GROW_ZONE) {
plantRepository.getPlants()
} else {
plantRepository.getPlantsWithGrowZoneNumber(it)
}
}
plantList.addSource(livePlantList, plantList::setValue)
}

fun getPlants() = plantList

fun setGrowZoneNumber(num: Int) {
growZoneNumber.value = num
}

fun clearGrowZoneNumber() {
growZoneNumber.value = NO_GROW_ZONE
}

fun isFiltered() = growZoneNumber.value != NO_GROW_ZONE

companion object {
private const val NO_GROW_ZONE = -1
}
 */
